const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading:true,
    card:{},
    coupon:{},
    cards:[],
    coupons:[],
    checkbox: [],
    coupon_id: [],
    coupon_ids: [], //最后选择的赠品ID
    coupon_count:0,
    card_id:0,
    store_id:0,
    page:0,
  },
  //监听页面初次加载
  onLoad: function (event) {
    app.getParam(event,(param) => {
      this.data.store_id = param.store_id
    });
  },
  //监听页面每次进入
  onShow: function () {
    app.isLogin(() => {
      this.getVip(this.data.store_id);
    });
  },
  //下拉
  onReachBottom: function () {
    this.setData({
      loading: true,
    });
    this.getCardCoupon();
  },
  //获取会员卡信息
  getVip: function (store_id) {
    var param = {
      signkey: app.util().getRandom(12),
      store_id: store_id
    }
    app.api().Get('api/v4/allwin/card/storeCard',param,(rel)=>{
      if (rel.code == 200){
       this.setData({
        cards:rel.data,
        card:rel.data[0],
        action:0,
       })
       this.data.card_id = rel.data[0].id;
       this.getCoupon(rel.data[0].coupon_id)
      }
    })
  },
  onCoupon(e){
    var action = e.currentTarget.dataset.action;
    this.setData({
      action:action,
      checkbox:[],
      coupon_id:[],
      coupon_ids:[],
      coupon_count:0,
      card:this.data.cards[action]
    })
    this.data.card_id = e.currentTarget.dataset.card_id;
    this.getCoupon(e.currentTarget.dataset.coupon_id);
  },
  //获取配套赠品
  getCoupon:function(id){
    var param = {
      signkey: app.util().getRandom(12),
      id: id
    }
    app.api().Get('api/v4/allwin/coupon/getCoupon',param,(rel)=>{
      this.setData({
        coupon: rel.data,
        coupons:[],
        page:0,
        loading:true,
      })
      this.getCardCoupon()
    })
  },
  //获取会员卡信息
  getCardCoupon: function () {
    if (this.data.loading) {
      var param = {
        signkey: app.util().getRandom(12),
        card_id: this.data.card_id,
        page:this.data.page+1
      }
      app.api().Get('api/v4/allwin/card/getCoupon',param,(result)=>{
        if (result.code == 200) {
          var coupons = this.data.coupons,
              checkbox = this.data.checkbox,
              coupon_id = this.data.coupon_id;
          for (let i in result.data) {
            coupons.push(result.data[i]);
            checkbox.push(false);
            coupon_id.push(0);
          }
          this.setData({
            coupons: coupons,
            checkbox: checkbox,   //选择状态
            page: param.page,
          });
        }
      })
      this.setData({
        loading: false
      })
    }
  },
  //选择赠品
  onCheckbox(event){
    var id = event.currentTarget.dataset.id;
    var key = event.currentTarget.dataset.key
    var checkbox = !this.data.checkbox[key];
    var card = this.data.card;
    if (checkbox == true) {
      var coupon_count = this.data.coupon_count;
      if (coupon_count == card.coupon_num) {
        app.wxAlert('仅允许选' + card.coupon_num + '张优惠或赠品券')
        return;
      }
    }  
    var coupon_id = this.data.coupon_id;
        coupon_id[key] = checkbox ? id : 0;
    var checkbox_array = "checkbox[" + key + "]";
    this.setData({
      [checkbox_array]: checkbox,
    });
    this.data.coupon_id = coupon_id;
    this.coupon_count();
  },
  //统计已选优惠券
  coupon_count: function () {
    var coupon = this.data.coupon_id;
    var len = [];
    var j = 0;
    for (var i in coupon) {
      if (coupon[i] != 0) {
        len[j] = coupon[i];
        j++
      }
    }
    this.data.coupon_ids = len;
    this.setData({
      coupon_count:len.length
    })
  },
  /**
   * 提交
   */
  onSubmit() {
    wx.showLoading({ title: '加载中', mask: true });
    var parms = {
      signkey: app.util().getRandom(12),
      card_id: this.data.card_id,
      store_id: this.data.store_id,
      coupon_ids: JSON.stringify(this.data.coupon_ids),
    }
    app.api().Post('api/v4/allwin/card/payment', parms, function (rel) {
      app.doWechatPay(rel.data,() => {
        wx.redirectTo({
          url: '../user/card',
        })
      },()=>{
        app.wxAlert('取消或支付失败');
      });
    })
    wx.hideLoading();
  },
  //分享按钮
  onShareAppMessage: function (res) {
    return {
      title: '我正在参加【'+this.data.coupon.name+'】的超值活动，邀你一起。',
      imageUrl: this.data.coupon.store.img,
      path: '/pages/card/store?ucode=' + app.globalData.loginuser.ucode+'store_id='+this.data.store_id
    }
  },
})